import java.io.*;
import java.nio.charset.Charset;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Batiuk
 * Date: 03.12.13
 * Time: 23:57
 * To change this template use File | Settings | File Templates.
 */
public class DataReaderWriterValidator {
	private static final String regex = "\\p{Punct}+|\\p{Punct}+(\\s+)|\\s+|\\p{Punct}+";
//	private static String currentCharset;

	public static Set<String> readFromStream(InputStream inputStream, String currentCharset) throws IOException {
		return readFromStream(inputStream, Charset.forName(currentCharset));
	}


	public static Set<String> readFromStream(InputStream inputStream, Charset currentCharset) throws IOException {
		byte[] buff;
		int available = 0;
		Set<String> setOfWords;
		Set<String> result = new HashSet<String>();
		BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
		available = bufferedInputStream.available();
		buff = new byte[available];
		bufferedInputStream.read(buff);
		String str1 = (new String(buff, currentCharset)).toLowerCase();
		setOfWords = new HashSet<String>(Arrays.asList(str1.split(regex)));
		for ( String string : setOfWords ) {
			if ( !string.equals("") && !string.matches(".*\\d.*") ) {

				result.add(string);
			}
		}
		return result;
	}


	public static void writeToStream(OutputStream outputStream, Map<Integer, Set<String>> result) throws IOException {
		DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
		Collection<Set<String>> settt = result.values();
		for ( Set<String> set : settt ) {
			if ( set.size() > 1 ) {
				dataOutputStream.writeUTF(set.toString() + "\r\n");
				dataOutputStream.flush();
			}
		}

	}

}
